package com.sise.parkinglot.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.sise.parkinglot.bean.CodeMsg;
import com.sise.parkinglot.bean.Day30DataBean;
import com.sise.parkinglot.bean.Day7DataBean;
import com.sise.parkinglot.composition.PublicService;
import com.sise.parkinglot.entity.Park;
import com.sise.parkinglot.entity.SitInfo;
import com.sise.parkinglot.entity.User;
import com.sise.parkinglot.mapper.ParkMapper;
import com.sise.parkinglot.mapper.SitInfoMapper;
import com.sise.parkinglot.mapper.UserMapper;
import com.sise.parkinglot.service.IIndexService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.text.SimpleDateFormat;
import java.util.*;
import java.util.concurrent.atomic.AtomicReference;

/**
 * @author ZJ
 */
@Service
public class IndexServiceImpl implements IIndexService {

    @Resource
    private UserMapper userMapper;

    @Resource
    private SitInfoMapper sitInfoMapper;

    @Resource
    private ParkMapper parkMapper;

    @Resource
    private PublicService publicService;

    @Override
    public CodeMsg<Map<String, Object>> init() {
        Map<String, Object> map = new HashMap<>();
        //用户数
        int userCount = Math.toIntExact(userMapper.selectCount(null));
        //车尾数
        int sitCount = Math.toIntExact(sitInfoMapper.selectCount(null));
        //剩余车位
        int surplusSitCount = Math.toIntExact(sitInfoMapper.selectCount(new QueryWrapper<SitInfo>().lambda().eq(SitInfo::getState, 1)));
        //今日收益
        SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
        List<Park> parkList = parkMapper.selectList(new QueryWrapper<Park>().lambda().likeRight(Park::getEndPark, formatter.format(new Date())));
        AtomicReference<BigDecimal> income = publicService.getIncome(parkList);

        //用户分类
        Map<String, Integer> userClassification = new HashMap<>();
        QueryWrapper<User> userQueryWrapper = new QueryWrapper<>();
        userQueryWrapper.lambda().eq(User::getCardType, 1);
        userClassification.put("common", Math.toIntExact(userMapper.selectCount(userQueryWrapper)));
        userQueryWrapper = new QueryWrapper<>();
        userQueryWrapper.lambda().eq(User::getCardType, 2);
        userClassification.put("vip", Math.toIntExact(userMapper.selectCount(userQueryWrapper)));

        //获取前7天数据
        List<Day7DataBean> day7Data = new ArrayList<>();
        for (int i = 6; i >= 0; i--) {
            for (int j = 1; j <= 3; j++) {
                int oneDay = parkMapper.getOneDayByStationType(j, i);
                Date pastDate = publicService.getPastDate(i);
                String weekOfDate = publicService.getWeekOfDate(pastDate);
                Day7DataBean day7DataBean = new Day7DataBean().setValue(oneDay).setDate(weekOfDate);
                if (j == 1) {
                    day7DataBean.setType("小型车");
                } else if (j == 2) {
                    day7DataBean.setType("中型车");
                } else {
                    day7DataBean.setType("大型车");
                }
                day7Data.add(day7DataBean);
            }
        }

        //获取前30天数据
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
        List<Day30DataBean> day30Data = new ArrayList<>();
        for (int i = 30; i >= 0; i--) {
            List<Park> oneDay = parkMapper.getOneDay(i);
            AtomicReference<BigDecimal> money = publicService.getIncome(oneDay);
            Date pastDate = publicService.getPastDate(i);
            Day30DataBean day30DataBean = new Day30DataBean()
                    .setValue(money.get().intValue()).setDate(simpleDateFormat.format(pastDate));
            day30Data.add(day30DataBean);
        }

        //数据
        map.put("userCount", userCount);
        map.put("sitCount", sitCount);
        map.put("surplusSitCount", surplusSitCount);
        map.put("income", income);
        map.put("userClassification", userClassification);
        map.put("day7Data", day7Data);
        map.put("day30Data", day30Data);
        return new CodeMsg<Map<String, Object>>().setCode(200).setMsg("获取成功").setData(map);
    }

}
